package com.example.blog.service;

import com.example.blog.mapper.BlogMapper;
import com.example.blog.mapper.UserMapper;
import com.example.blog.model.BlogInfo;
import com.example.blog.model.UserInfo;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BlogMapper blogMapper;

    public UserInfo selectByName(String username){
        return userMapper.SelectByName(username);
    }

    public List<BlogInfo> getList() {
        return  blogMapper.selectAll();
    }

    public BlogInfo getBlogDetail(Integer blogId) {
        return blogMapper.selectBlodId(blogId);
    }

    public UserInfo selectUserInfoById(Integer userId) {
        return userMapper.SelectById(userId);
    }

    public UserInfo getUserInfoByBlogId(Integer blogId) {
        BlogInfo blogInfo = blogMapper.selectBlodId(blogId);
        if(blogInfo!=null||blogInfo.getUserId()>0){
            return userMapper.SelectById(blogInfo.getUserId());
        }
        else {
            return null;
        }
    }

    public int insertBlog(BlogInfo blogInfo) {
        return blogMapper.insertBlog(blogInfo);
    }

    public void updateBlog(BlogInfo blogInfo) {
        blogMapper.updateBlog(blogInfo);
    }
}
